/**
 * @Created Nov 24, 2010 10:52:38 AM
 * @author cry30
 */
package com.philip.journal.core.service;

import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.philip.core.BusinessServiceProxy;
import com.philip.journal.core.Constant;
import com.philip.journal.core.JournalTestCommon;
import com.philip.journal.core.bean.User;
import com.philip.journal.core.dao.DAOFacade;

/**
 * Base class for all DAO unit test class.
 *
 * Use {@link #getSession()} as the first parameter to the method call.
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "/test/config/serviceContext.xml" })
//{"classpath*:spring/mocksContext.xml"})
public class AbstractSpringJournalTestService extends AbstractJUnit4SpringContextTests {

    /** IOC Wired test DAO Facade instance. */
    @Autowired
    private transient DAOFacade daoFacade;

    /** Test Business Session Data. */
    private transient Map<String, Object> session;

    /** Refactored common functionality. */
    private final JournalTestCommon common = new JournalTestCommon();

    /** Initialize user, session and facade for test subclass reuse. */
    @Before
    public void setUp() {
        setSession(new HashMap<String, Object>());
        final User user = new User("TEST_USERNAME", "password", "checksum");
        getSession().put(Constant.CURRENT_USER, user);

        final ServiceFacade mockedFacade = Mockito.mock(ServiceFacade.class);
        getSession().put(BusinessServiceProxy.SERVICE_FACADE, mockedFacade);
    }

    /** Finalize DAO's for test subclass reuse. */
    @After
    public void tearDown() {
        //Need to reset container instantiated mocks.
        Mockito.reset(getDaoFacade().getUserDAO());
        Mockito.reset(getDaoFacade().getBranchDAO());
        Mockito.reset(getDaoFacade().getConfigItemDAO());
        Mockito.reset(getDaoFacade().getEntryDAO());
    }

    protected User getTestUser() {
        return (User) getSession().get(Constant.CURRENT_USER);
    }

    /**
     * @return the daoFacade
     */
    public DAOFacade getDaoFacade() {
        return daoFacade;
    }

    /** @return the session */
    public Map<String, Object> getSession() {
        return session;
    }

    /** @param pSession the session to set. */
    public void setSession(final Map<String, Object> pSession) {
        this.session = pSession;
    }

    protected JournalTestCommon getCommon() {
        return common;
    }
}
